// https://leetcode.cn/problems/contains-duplicate-ii/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        int n = nums.size();
        unordered_map<int, int> mp;

        for (int i = 0; i < n; i++) {
            if (mp.count(nums[i]) && abs(i - mp[nums[i]]) <= k) {
                return true;
            }
            mp[nums[i]] = i;
        }
        return false;
    }
};